/* @author adminz
 *  @created on 17.12.2008 18:43:51
 */
package de.fhhannover.inform.wari.grp8.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class LoggerGui extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JTextArea outputTxtArea;
	private JScrollPane scrollPane;
	private JPanel southPanel;
	private JButton closeBtn, deleteBtn;
	File datei;
	FileReader fr;
	static FileWriter fw;
	int read;
	Container con;

	public LoggerGui() throws IOException {
		// super(title);
		this.setLayout(null);
		this.setTitle("Wari G8 Logger");
		datei = new File("warig8.log");
		if (!datei.exists())
			datei.createNewFile();
		fr = new FileReader(datei);
		southPanel = new JPanel();
		// southPanel.setBackground(Color.GREEN);
		southPanel.setBounds(0, 330, 650, 40);
		closeBtn = new JButton("Schliessen");
		deleteBtn = new JButton("Leeren");
		closeBtn.setBounds(200, 340, 100, 25);
		deleteBtn.setBounds(310, 340, 100, 25);
		con = this.getContentPane();
		this.setResizable(false);
		setSize(800, 400);
		setLocation(340, 130);
		outputTxtArea = new JTextArea(100, 10);
		scrollPane = new JScrollPane(outputTxtArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		outputTxtArea.setEditable(false);
		outputTxtArea.setFont(new Font("Arial", 1, 14)); // Name,style,size
		scrollPane.setBounds(0, 0, 793, 330);
		closeBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		con.add(scrollPane);
		// southPanel.setPreferredSize(new Dimension(650,10));
		con.add(closeBtn);
		con.add(deleteBtn);
		con.add(southPanel);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		// showText();
	}

	// public static void main(String args[]) throws IOException {
	// Logger mylog = new Logger("Wari G8 Logger");
	// //mylog.writeIntoFile("Hellow world");
	// mylog.writeIntoFile("Hellow world");
	// mylog.setVisible(true);
	// // mylog.showText();
	// }
	public void writeIntoTextArea(String str) {
		// outputTxtArea.setText(this.toString());
		// while ((read = fr.read()) != -1) {
		// // outputTxtArea.setText(""+(char)read);
		// outputTxtArea.append("" + (char) read);
		// }
		// fr.close();
		outputTxtArea.append(str);
	}

	public void truncate() {
		// fw = new FileWriter(datei);
		// fw.write("");
		// fw.close();
		outputTxtArea.setText("");
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == closeBtn)
			this.dispose();
		else if (source == deleteBtn)
			truncate();
	}

}
